package com.topc.participant.api;

import cn.hutool.core.util.ObjectUtil;
import com.topc.base.api.CommonResult;
import com.topc.participant.model.dto.SaveAdminDto;
import com.topc.participant.model.dto.UpdatePasswordDto;
import com.topc.participant.model.po.AdminInfo;
import com.topc.participant.model.vo.AdminDetailVo;
import com.topc.participant.model.vo.AdminProjectDynamicVo;
import com.topc.participant.model.vo.AdminShowVo;
import com.topc.participant.service.IAdminInfoService;
import com.topc.participant.util.MinioUtils;
import com.topc.participant.util.SecurityUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api("管理员业务封闭接口")
@Slf4j
@RestController
@RequestMapping("/close/admin")
public class AdminCloseController {

    @Autowired
    private IAdminInfoService adminInfoService;

    @Autowired
    private MinioUtils minioUtils;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<AdminInfo> getAdminInfoLocal() {
        AdminDetailVo adminInfo = adminInfoService.getAdminInfoByNumber(SecurityUtil.getUser().getNumber());
        return CommonResult.success(adminInfo);
    }

    @RequestMapping(value = "/upload-avatar", method = RequestMethod.POST)
    public CommonResult<String> getUploadFile(@RequestParam("file") MultipartFile file) {
        String newPath = minioUtils.uploadOne(file);
        String originPath = adminInfoService.setAdminAvatar(newPath, SecurityUtil.getUser().getNumber());
        minioUtils.removeOne(originPath);
        return CommonResult.success(newPath);
    }

    @RequestMapping(value = "/update-password", method = RequestMethod.GET)
    public CommonResult<String> updatePassword(@Validated UpdatePasswordDto passwordDto) {
        if (ObjectUtil.notEqual(passwordDto.getOldPassword(), passwordDto.getReOldPassword()))
            return CommonResult.failed("两次密码输入不同");
        if (passwordDto.getNewPassword().length() < 6)
            return CommonResult.failed("不符合密码设置要求");
        adminInfoService.updatePassword(passwordDto, SecurityUtil.getUser().getNumber());
        return CommonResult.success(null);
    }

    @RequestMapping(value = "/show-team", method = RequestMethod.GET)
    public CommonResult<List<AdminShowVo>> showAdminTeam() {
        List<AdminShowVo> adminTeamVos = adminInfoService.showAdminTeam();
        return CommonResult.success(adminTeamVos);
    }

    @RequestMapping(value = "/show-dynamic", method = RequestMethod.GET)
    public CommonResult<List<AdminProjectDynamicVo>> getProjectDynamic() {
        List<AdminProjectDynamicVo> projectDynamicVos = adminInfoService.getProjectDynamic();
        return CommonResult.success(projectDynamicVos);
    }

    @PostMapping("/save")
    public CommonResult<Long> saveAdminInfo(@RequestBody @Validated SaveAdminDto saveAdminDto) {
        Long newId = adminInfoService.saveAdminInfo(saveAdminDto);
        return CommonResult.success(newId);
    }

    @PostMapping("/update")
    public CommonResult<String> updateAdminInfo(@RequestBody @Validated SaveAdminDto saveAdminDto) {
        adminInfoService.updateAdminInfo(saveAdminDto);
        return CommonResult.success(null);
    }

}
